package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjromotion;
import com.chezhujie.www.domain.vo.FestSelectVO;
import com.chezhujie.www.service.CzjromotionService;
import com.xy.core.page.Pagination;
import com.xy.core.utils.ConvertObj;
import com.xy.core.utils.UUIDUtils;
import com.xy.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   扫描-活动/奖品统计
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjromotion")
public class CzjromotionController extends BaseController<Czjromotion> {
    @Autowired
    private CzjromotionService czjromotionService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjromotion czjromotion, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjromotion,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjromotion);
          // Pagination pagination = czjromotionService.list(pageNo.intValue(), pageSize.intValue(), params, model);
           Pagination pagination = czjromotionService.listByPhone(pageNo.intValue(), pageSize.intValue(), params, model);
            List<FestSelectVO> vo=czjromotionService.getFestSelect(); // 活动下拉列表 
            model.addAttribute("vo", vo);
            
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取扫描-活动/奖品统计列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjromotion);
        return "czjromotion/list";
    }
    
    /** 活动列表 **/
    @RequestMapping("/listDetail.do")
    public String listDetail(Integer pageNo, Integer pageSize, Czjromotion czjromotion, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjromotion,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        
        try {
            Map<String, Object> params = ConvertObj.toMap(czjromotion);
            Pagination pagination = czjromotionService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            List<FestSelectVO> vo=czjromotionService.getFestSelect(); // 活动下拉列表 
            model.addAttribute("vo", vo);
            
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取扫描-活动/奖品统计列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjromotion);
        return "czjromotion/listDetail";
    }
    /**
    * api接口扫描-活动/奖品统计列表
    * @param pageNo
    * @param pageSize
    * @param czjromotion
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiList.jn")
    public void apiList(Integer pageNo, Integer pageSize, Czjromotion czjromotion,
        HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjromotion);
            Pagination pagination = czjromotionService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取扫描-活动/奖品统计列表失败!"+e,"",resp);
        }

    }
    /**
    * api接口扫描-活动/奖品统计详情
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiDetail.jn")
    public void apiDetail(HttpServletRequest req, HttpServletResponse resp,
    ModelMap model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        Enumeration<String> enumeration = req.getParameterNames();
        if(enumeration.hasMoreElements()){
            String next = enumeration.nextElement();
            params.put(next, req.getParameter(next));
        }

        try {
            Map<String,Object> result = czjromotionService.getApiDetail(params);
            jsonResult(1,"","",result,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取扫描-活动/奖品统计详情失败!"+e,"",resp);
        }

    }
    /** api测试页面 **/
    @RequestMapping("/apiTest.do")
    public String apiList(HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        model.addAttribute("entity",new HashMap<String,Object>());
        return "czjromotion/apiTest";

    }

    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjromotion czjromotion,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjromotion.getRomotionId())){
                czjromotionService.update(czjromotion);


            }else{
                czjromotion.setRomotionId(UUIDUtils.getUUID());
                czjromotionService.insert(czjromotion);
            }
            model.addAttribute("success", "保存扫描-活动/奖品统计成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存扫描-活动/奖品统计失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String romotionId,Czjromotion czjromotion, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjromotion,req,model);
        try {
            Czjromotion entity = czjromotionService.queryById(czjromotion.getRomotionId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询扫描-活动/奖品统计出错!详情："+e.getMessage());
        }
        return "czjromotion/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjromotion czjromotion, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjromotion,req,model);
        if(StringUtils.isNotBlank(czjromotion.getRomotionId())){
            czjromotion = czjromotionService.queryById(czjromotion.getRomotionId());
            model.addAttribute("entity", czjromotion);
        }
        return "czjromotion/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String romotionId,Czjromotion czjromotion, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjromotionService.deleteById(czjromotion.getRomotionId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }

    private void setMsg(Czjromotion czjromotion,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjromotion);
    }
}
